package cx.zan.dao;

import cx.zan.domain.Category;
import cx.zan.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/4/1 9:22
 */
public class CategoryDao {
    /**
     * 查询所有分类信息
     *
     * @return
     */
    public List<Category> findAll() {
        //注入数据源
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());

        String sql = "select * from category";

        List<Category> list = null;
        try {
            // 查询 集合使用这个
            list = queryRunner.query(sql, new BeanListHandler<>(Category.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除单个
     *
     * @param cid
     */
    public void delete(String cid) {
        //注入数据源
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "delete from category where cid=?";
        try {
            // 查询 集合使用这个
            queryRunner.update(sql, cid);

        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过cid查询单个
     *
     * @param cid
     * @return
     */
    public Category findById(String cid) {
        //注入数据源
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select * from category where cid=?";
        try {
            //
            Category category = queryRunner.query(sql, new BeanHandler<>(Category.class), cid);
            return category;
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常
            throw new RuntimeException(e);
        }
    }

    /**
     * 插入分类信息
     *
     * @param category
     */
    public void insert(Category category) {
        //注入数据源
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "insert into category values(?,?)";
        try {
            //插入 使用 update
            queryRunner.update(sql, category.getCid(), category.getCname());
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改分类信息
     *
     * @param category
     */
    public void update(Category category) {
        //注入数据源
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "update category set cname=? where cid=?";
        try {
            //
            queryRunner.update(sql, category.getCname(), category.getCid());
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常
            throw new RuntimeException(e);
        }
    }
}